package com.cn.wanxi.servlet.back.nav;

import com.alibaba.fastjson.JSONObject;
import com.cn.wanxi.model.NavModel;
import com.cn.wanxi.model.PageModel;
import com.cn.wanxi.service.INavService;
import com.cn.wanxi.service.impl.NavServiceImpl;
import com.cn.wanxi.util.DataEncoding;
import com.cn.wanxi.util.JDBCUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/back/nav/info")
public class NavInfoServlet extends HttpServlet {
    INavService navService = new NavServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        DataEncoding.setJsonContentType(resp);
        DataEncoding.setCharacterEncoding(req,resp);
        //导航栏详情
        String nid = req.getParameter("id");
        int id = 0;
        if (!"".equals(nid))
            id = Integer.parseInt(nid);
        NavModel navModel = navService.getNav(id);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("nav",navModel);
        resp.getWriter().print(jsonObject);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        DataEncoding.setJsonContentType(resp);
        DataEncoding.setCharacterEncoding(req,resp);
        //导航栏分页列表和模糊查询
        String title = req.getParameter("title");
        String pageSize = req.getParameter("page");
        String pageNum = req.getParameter("size");
        String sort = req.getParameter("sort");
        int page = Integer.parseInt(pageSize);
        int size = Integer.parseInt(pageNum);
        Map<String,Object> map = new HashMap<>();
        map.put("title",title);
        map.put("sort",sort);
        map.put("page",(page-1)*size);
        map.put("size",size);
        PageModel<NavModel> navList = navService.getNavs(map);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("navs",navList);
        resp.getWriter().print(jsonObject);
    }
}
